package com.gulimall.ware.service.impl;

import com.baomidou.mybatisplus.annotation.TableLogic;
import com.common.to.SkuInfoEntity;
import com.common.utils.R;
import com.gulimall.ware.feign.ProductFeignService;
import com.gulimall.ware.vo.SkuHasStockVo;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.common.utils.PageUtils;
import com.common.utils.Query;

import com.gulimall.ware.dao.WareSkuDao;
import com.gulimall.ware.entity.WareSkuEntity;
import com.gulimall.ware.service.WareSkuService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;


@Service("wareSkuService")
public class WareSkuServiceImpl extends ServiceImpl<WareSkuDao, WareSkuEntity> implements WareSkuService {

    @Autowired
    private WareSkuDao wareSkuDao;


    @Autowired
    private ProductFeignService productFeignService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {

        // 仓库Id
        String wareId = (String) params.get("wareId");
        // skuId
        String skuId = (String) params.get("skuId");

        QueryWrapper<WareSkuEntity> wrapper = new QueryWrapper<>();

        if (StringUtils.isNotBlank(wareId)) {
            wrapper.eq("ware_id", wareId);
        }
        if (StringUtils.isNotBlank(skuId)) {
            wrapper.eq("sku_id", skuId);
        }

        IPage<WareSkuEntity> page = this.page(new Query<WareSkuEntity>().getPage(params), wrapper);

        return new PageUtils(page);
    }

    @Transactional
    @Override
    public void addStock(Long skuId, Long wareId, Integer skuNum) {

        // 判断 如果新增还是修改
        List<WareSkuEntity> wareSkuEntities = wareSkuDao.selectList(new QueryWrapper<WareSkuEntity>().eq("sku_id", skuId).eq("ware_id", wareId));
        if (CollectionUtils.isEmpty(wareSkuEntities)) {
            // 新增
            WareSkuEntity wareSkuEntity = new WareSkuEntity();
            wareSkuEntity.setSkuId(skuId);
            wareSkuEntity.setWareId(wareId);
            wareSkuEntity.setStock(skuNum);
            wareSkuEntity.setStockLocked(0);

            // todo:远程查询sku名字
            R info = productFeignService.info(skuId);
            try {
                if (info.getCode() == 0) {
                    SkuInfoEntity key = (SkuInfoEntity) info.get("data");
                    String skuName = key.getSkuName();
                    wareSkuEntity.setSkuName(skuName);
                }
            } catch (Exception e) {
                // 保证事务不生效
            }

            wareSkuDao.insert(wareSkuEntity);
        } else {
            wareSkuDao.addStock(skuId, wareId, skuNum);
        }

    }

    @Override
    public List<SkuHasStockVo> getSkuHasStock(List<Long> skuIds) {

        if (CollectionUtils.isEmpty(skuIds)) {
            return Collections.emptyList();
        } else {
            return skuIds.stream().map(skuId -> {
                SkuHasStockVo vo = new SkuHasStockVo();
                Long count = wareSkuDao.getSkuHasStock(skuId);
                vo.setHasStock(count == null ? false : count > 0);
                vo.setSkuId(skuId);
                return vo;
            }).collect(Collectors.toList());
        }
    }

}